<?php

/**
 * Shopalong currency class
 *
 * Class to handle currencies
 * TABLES: currencies
 *
 * $Id: currencies.php,v 1.16 2003/06/05 23:16:46 hpdl Exp $
 * Copyright (c) 2003 osCommerce
 * Portions Copyright (c) 2006 Shopalong
 *
 * Released under the GNU General Public License
 */

class Currencies
{
	var $_currencies;
	
	/**
	 * Constructor function
	 *
	 * @access public
	 */
	
	function Currencies()
	{
		global $currency, $session;
		
		if (!$session->isRegistered('currency'))
		{
			$session->register('currency');
			$currency = DEFAULT_CURRENCY;
		}
		
		if ((USE_DEFAULT_LANGUAGE_CURRENCY == 'true') && (LANGUAGE_CURRENCY != $currency))
		{
			$currency = (USE_DEFAULT_LANGUAGE_CURRENCY == 'true') ? LANGUAGE_CURRENCY : DEFAULT_CURRENCY;
		}
		
		$this->_currencies = array();
		$currencies_query = tep_db_query('select code, title, symbol_left, symbol_right, decimal_point, thousands_point, decimal_places, value from '.TABLE_CURRENCIES);
		
		while ($currencies = tep_db_fetch_array($currencies_query))
		{
			$this->_currencies[$currencies['code']] = array(
				'title' => $currencies['title'],
				'symbol_left' => $currencies['symbol_left'],
				'symbol_right' => $currencies['symbol_right'],
				'decimal_point' => $currencies['decimal_point'],
				'thousands_point' => $currencies['thousands_point'],
				'decimal_places' => $currencies['decimal_places'],
				'value' => $currencies['value']
			);
		}
	}
	
	/**
	 * Display a price
	 *
	 * @access public
	 * @param integer $productsPrice The price to be displayed
	 * @param integer $productsTax Default: 0
	 * @param integer $quantity Default: 1
	 * @return string The formatted price
	 */
	
    function displayPrice($productsPrice, $productsTax=-1, $quantity=1)
	{
		if ($productsTax >= 0)
		{
			$productsPrice = tep_add_tax($productsPrice, $productsTax);
		}
		
		return $this->format($productsPrice * $quantity);
	}
	
	/**
	 * Does a currency exist in the system?
	 *
	 * @access public
	 * @param string $code The currency code to check (ISO2 code)
	 * @return boolean Whether the currency exists in the system
	 */
	
    function exists($code)
	{
		if (isset($this->_currencies[$code]) && tep_not_null($this->_currencies[$code]))
		{
			return true;
		}
		
		else
		{
			return false;
		}
	}
	
	/**
	 * Format a number for a given currency
	 *
	 * @access public
	 * @param integer $number The number to be formatted
	 * @param boolean $calculateCurrencyValue Calculate currency value? (default true)
	 * @param string $currencyType The currency type to use (default empty)
	 * @param integer $currencyValue The value of the currency (default empty)
	 * @return string The formatted number
	 */
	
	function format($number, $calculateCurrencyValue = true, $currencyType = '', $currencyValue = '')
	{
		global $currency;
		
		if (empty($currencyType))
		{
			$currencyType = $currency;
		}
		
		if ($calculateCurrencyValue == true)
		{
			$rate = (tep_not_null($currencyValue)) ? $currencyValue : $this->_currencies[$currencyType]['value'];
			$format_string = $this->_currencies[$currencyType]['symbol_left'].number_format(
				tep_round($number*$rate, $this->_currencies[$currencyType]['decimal_places']),
				$this->_currencies[$currencyType]['decimal_places'],
				$this->_currencies[$currencyType]['decimal_point'],
				$this->_currencies[$currencyType]['thousands_point']
			).$this->_currencies[$currencyType]['symbol_right'];
			
			// if the selected currency is in the european euro-conversion and the default currency is euro,
			// the currency will displayed in the national currency and euro currency
			
			if ( (DEFAULT_CURRENCY == 'EUR') && ($currencyType == 'DEM' || $currencyType == 'BEF' || $currencyType == 'LUF' || $currencyType == 'ESP' || $currencyType == 'FRF' || $currencyType == 'IEP' || $currencyType == 'ITL' || $currencyType == 'NLG' || $currencyType == 'ATS' || $currencyType == 'PTE' || $currencyType == 'FIM' || $currencyType == 'GRD') )
			{
				$format_string .= ' <small>[' . $this->format($number, true, 'EUR') . ']</small>';
			}
		}
		
		else
		{
			$format_string = $this->_currencies[$currencyType]['symbol_left'].number_format(
				tep_round($number, $this->_currencies[$currencyType]['decimal_places']),
				$this->_currencies[$currencyType]['decimal_places'],
				$this->_currencies[$currencyType]['decimal_point'],
				$this->_currencies[$currencyType]['thousands_point']
			).$this->_currencies[$currencyType]['symbol_right'];
		}
		
		return $format_string;
	}
	
	/**
	 * Get the current currency code
	 *
	 * @access public
	 * @return string The current currency code
	 */
	
	function getCurrentCode()
	{
		global $currency;
		echo $currency;
		return $currency;
	}
	
	/**
	 * Get the current currency rate
	 *
	 * @access public
	 * @return double The current currency rate
	 */
	
	function getCurrentRate()
	{
		global $currency;
		return $this->getValue($currency);
	}
	
	/**
	 * Get the current currency's decimal places
	 *
	 * @access public
	 * @return integer The current currency's decimal places
	 */
	
	function getCurrentDecimalPlaces()
	{
		global $currency;
		return $this->getDecimalPlaces($currency);
	}
	
	/**
	 * Get a currencies decimal places
	 *
	 * @access public
	 * @param string $code The currency code to get (ISO2 code)
	 * @return integer The currency's decimal places
	 */
	
	function getDecimalPlaces($code)
	{
		return $this->_currencies[$code]['decimal_places'];
	}
	
	/**
	 * Get a currency's value
	 *
	 * @access public
	 * @param string $code The currency code to get (ISO2 code)
	 * @return float The currency's value
	 */
	
	function getValue($code)
	{
		return $this->_currencies[$code]['value'];
	}
	
	/**
	 * Set the currency
	 *
	 * @access public
	 * @param string $code The currency code to use (ISO2 code)
	 */
	
	function setCurrency($code)
	{
		global $currency;
		
		if (tep_currency_exists($code))
		{
			$currency = (USE_DEFAULT_LANGUAGE_CURRENCY == 'true') ? LANGUAGE_CURRENCY : $code;
		}
	}
}

?>
